<?php
/**
 * 项目管理
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use Think\Db;
use app\api\controller\common\CommonPlus;
class Project extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Project');
    }

    /**
     * 项目列表
     */
    public function projectList(){

        $keyword = $this->request->param('keyword');
        $page = $this->request->param('page');

        $page = max(1,intval($page));
        $pagesize = empty(input('pageSize'))?10:input('pageSize');

        $starttime = input('starttime');
        $endtime = input('endtime');

        $where['a.is_delete'] = 0;
        if(!empty($keyword)){
            $where['title|title_en'] = array('like','%'.$keyword.'%');
        }

        if(!empty($starttime)&&!empty($endtime)){
            $where['a.create_time'] = array(array('EGT',strtotime($starttime)),array('ELT',strtotime($endtime)+86400));
        }elseif(!empty($starttime)){
            $where['a.create_time'] = array('EGT',strtotime($starttime));
        }elseif(!empty($endtime)){
            $where['a.create_time'] = array('ELT',strtotime($endtime));
        }

        //$list = $this->model->listWherePage($where,'id desc',);
        $list = db('project')
                ->alias('a')
                ->field('a.*,b.account')
                ->join('admin b','a.create_uid=b.id')
                 ->order('id desc')->limit(($page-1)*$pagesize.','.$pagesize)
                ->where($where)->select();
        $total = db('project')->alias('a')->field('a.*,b.account')->join('admin b','a.create_uid=b.id')->where($where)->count();

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }

    /**
     * 项目详情
     * @return \think\response\Json|void
     */
    public function projectDetail(){
        $id = $this->request->param('id');

        $where['id'] = $id;
        $where['is_delete'] = 0;
        $info = $this->model->findWhere($where);

        return json(['code'=>1,'msg'=>'success','data'=>$info]);
    }

    /**
     * 新增项目
     * @return \think\response\Json|void
     */
    public function addProject(){
        $id = $this->request->param('id');
        $title = $this->request->param('title');
        $title_en = $this->request->param('title_en');
        $create_uid  = $this->admin['id'];
        $content = $this->request->param('content');

        $data = ['title'=>$title,'title_en'=>$title_en,'create_uid'=>$create_uid,'content'=>$content];

        $data['create_uid'] = $create_uid;
        $data['create_time'] = time();

        $r = $this->model->insertData($data);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }


        return json(['code'=>1,'msg'=>'操作成功']);
    }

    public function editProject(){
        $id = $this->request->param('id');
        $title = $this->request->param('title');
        $title_en = $this->request->param('title_en');
        $create_uid  = $this->admin['id'];
        $content = $this->request->param('content');
        $data = ['title'=>$title,'title_en'=>$title_en,'create_uid'=>$create_uid,'content'=>$content];

        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误，请核查']);
        }

        $where['id'] = $id;
        $r = $this->model->updateBywhere($data,$where);

        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    /**
     * 删除项目
     */
    public function delProject(){
        $ids = $this->request->param('id');
        $id = explode(',',$ids);
        $is=true;
        //首先进行数据验证
        foreach ($id as $val){
            $where = ['id'=>['in',$val],'is_delete'=>0];
            $r = $this->model->countWhere($where);
            if(empty($r)){
                $is=false;
                break;
            }
        }
        if(!$is){
            return json(['code'=>0,'msg'=>'记录不存在，请核实']);
        }
        $msg='';
        foreach ($id as $v){
            $r = $this->model->deleteWhere(['id'=>$v]);
            if(!$r){
                $is=false;
                $msg='id:'.$v.'操作失败';
            }
        }

        if(!$is){
            Db::rollback();
            return json(['code'=>0,'msg'=>$msg]);
        }
        Db::commit();
        return json(['code'=>1,'msg'=>'操作成功']);
    }

}